package cn.jietuo.first.class05;

import java.util.Arrays;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/7/11
 * @description: 基数排序
 */
public class Code03_RadixSort {

    /**
     * 针对非负数组的排序
     *
     * @param arr
     */
    public static void radixSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        radixSort(arr, 0, arr.length, maxBits(arr));
    }

    private static void radixSort(int[] arr, int l, int r, int digit) {
        // 默认中间操作数组长度 0-9
        final int radix = 10;
        // 准备辅助空间数组
        int[] help = new int[r - l + 1];
        for (int d = 1; d <= digit; d++) {
            // 每个位数上面的数字有多少个 0-9
            int[] count = new int[radix];
            for (int j = l; j < r; j++) {
                count[getDigitNum(arr[j], d)]++;
            }
            // 将数组转换
            for (int i = 1; i < count.length; i++) {
                count[i] = count[i] + count[i - 1];
            }
            // 添加
            for (int i = r - 1; i >= l; i--) {
                int j = getDigitNum(arr[i], d);
                help[count[j] - 1] = arr[i];
                count[j]--;
            }
            for (int i = l; i < r; i++) {
                arr[i] = help[i];
            }
        }
    }

    private static int getDigitNum(int num, int digit) {
        return (int) ((num / (Math.pow(10, digit - 1))) % 10);
    }

    private static int maxBits(int[] arr) {
        // step1: 求出最大数值的位数
        int max = Integer.MAX_VALUE;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
        }
        int digit = 0;
        while (max != 0) {
            digit++;
            max = max / 10;
        }
        return digit;
    }


    // for test
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100000;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            radixSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        printArray(arr);
        radixSort(arr);
        printArray(arr);

    }
}
    